﻿using System;
using System.Collections.Generic;
using XRM.Security.DataAccess;
using XRM.Security.WebUI.Controllers;

namespace XRM.Security.WebUI.Models
{
    public class BaseViewModel
    {
        public int? Id { get; set; }
        public bool IsEdit 
        {
            get
            {
                return Id.HasValue && Id > 0;
            }
        }
        public bool ReadOnly { get; set; }
        
        protected BaseController Controller
        {
            get;
            set;
        }

        protected IDataManager DataManager
        {
            get { return Controller.DataManager; }
        }

        public void SetController( BaseController controller )
        {
            Controller = controller;
        }

        public virtual void Init(IDataManager dataManager)
        {   
        }
    }

    public class FileUploaderModel : BaseViewModel
    {
        public string BaseName { get; set; }
        public string UploadUrl { get; set; }
        public bool IsOneFile { get; set; }
    }

    public class AutocompleteItem
    {
        public int Id { get; set; }
        public string Label { get; set; }
        public string Label2 { get; set; }
    }

    // Custom comparer for the Product class 
    class AutocompleteItemComparer : IEqualityComparer<AutocompleteItem>
    {
        public bool Equals(AutocompleteItem x, AutocompleteItem y)
        {
            if (Object.ReferenceEquals(x, y)) return true;

            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            return x.Id == y.Id && x.Label == y.Label;
        }

        public int GetHashCode(AutocompleteItem item)
        {
            if (Object.ReferenceEquals(item, null)) return 0;

            int hashLabel = item.Label == null ? 0 : item.Label.GetHashCode();
            int hashId = item.Id.GetHashCode();

            return hashLabel ^ hashId;
        }

    }
}